package com.shujia.Day07;

public class Demo01Object2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student = new Student("李玉龙", "bigdata30");
        Student clone = (Student)student.clone();
        // TODO 内存地址不同
        System.out.println(student);
        System.out.println(clone);
        System.out.println(student.equals(clone)); // true

        // 将student变量的引用赋值为null 那么 new Student对象就没有引用了 => 通过GC对其进行垃圾回收
        student = null;
        // 手动调用GC
        System.gc();
        System.out.println("系统执行完成...");
    }
}

class Student implements Cloneable {
    String name;
    String clazz;

    public Student(String name, String clazz) {
        this.name = name;
        this.clazz = clazz;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    /*
       TODO clone方法是 克隆出一个新的 对象
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    /*
        当对象需要会回收时，要调用该方法..
     */
    @Override
    protected void finalize() throws Throwable {
        System.out.println("当前对象被回收了");
    }

    @Override
    public boolean equals(Object otherObj) {
        Student otherStu = (Student)otherObj;
        return otherStu.name.equals(this.name)?otherStu.clazz.equals(this.clazz): false;
    }
}
